/*
 * GeonamesClient.java
 *
 * Created on 6 f�vrier 2008, 10:45
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.great.functionalCore;

import java.net.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geonames.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import br.great.beans.Wiki;

/**
 *
 * @author defautce
 */
public class GeonamesClient {
    private static String USER_AGENT = "geonames webservice client 0.5";
    private static String GEONAMES_SERVER = "http://ws.geonames.org";
    
    /** Creates a new instance of GeonamesClient */
    public GeonamesClient() {
    }
    
    public static Vector<Wiki> findNearbyWikipedia(double latitude,double longitude, String language) {
        Vector<Wiki> articles = new Vector<Wiki>();
        try {
            String url = GEONAMES_SERVER + "/findNearbyWikipedia?";
            url = url + "lat=" + latitude;
            url = url + "&lng=" + longitude;
            url = url + "&maxRows=10";
            if (language != null) {
            	url = url + "&lang=" + language;
            }
            
//System.out.println(url);

            URLConnection conn = new URL(url).openConnection();
            conn.setRequestProperty("User-Agent", USER_AGENT);
            SAXBuilder parser = new SAXBuilder();
            Document doc = parser.build(conn.getInputStream());
            
            Element root = doc.getRootElement();
            for (Object obj : root.getChildren("entry")) {
            	Element codeElement = (Element) obj;
                if(Double.parseDouble(codeElement.getChildText("distance"))<5){
                    Wiki wikipediaArticle = new Wiki();
                    wikipediaArticle.setLanguage(codeElement.getChildText("lang"));
                    wikipediaArticle.setTitle(codeElement.getChildText("title"));
//System.out.println("The Wiki we found is "+codeElement.getChildText("title"));
                    wikipediaArticle.setSummary(codeElement.getChildText("summary"));
                    wikipediaArticle.setFeature(codeElement.getChildText("feature"));
                    wikipediaArticle.setWikipediaUrl(codeElement.getChildText("wikipediaUrl"));
                    wikipediaArticle.setLatitude(Double.parseDouble(codeElement.getChildText("lat")));
                    wikipediaArticle.setLongitude(Double.parseDouble(codeElement.getChildText("lng")));
                    wikipediaArticle.setDistance(Double.parseDouble(codeElement.getChildText("distance")));
                    String population = codeElement.getChildText("population");
                    if (population != null && !"".equals(population)) {
                        wikipediaArticle.setPopulation(Integer.parseInt(population));
                    }
                    String elevation = codeElement.getChildText("altitude");
                    if (elevation != null && !"".equals(elevation)) {
                        wikipediaArticle.setElevation(Integer.parseInt(elevation));
                    }
                    wikipediaArticle.setRELATION_TYPE("near");
                    articles.add(wikipediaArticle);
                }
            }
            return articles;
        } catch (Exception ex) {
            Logger.getLogger(GeonamesClient.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return articles;
    }
    
    public static Vector<Wiki> findNearbyWikipedia(double latitude,double longitude) {        
        return findNearbyWikipedia(latitude, longitude, "fr");
        
    }

    /*public static void main (String[] args){
        GeonamesClient ws = new GeonamesClient();
        List<WikipediaArticle> articles = new ArrayList<WikipediaArticle>();
        try {
            articles = ws.findNearbyWikipedia(41.890159606933594, 12.490496635437012);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Iterator it = articles.iterator();
        while(it.hasNext()){
            WikipediaArticle article = (WikipediaArticle) it.next();
            System.out.println("title : " +article.getTitle());
            System.out.println("summary : " +article.getSummary());
            System.out.println("url : " +article.getWikipediaUrl());
            System.out.println("--------------------------------------");
        }
    }*/
}
